package com.smart.wap.work;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.domain.LoginResult;
import com.smart.common.exception.SmartException;
import com.smart.utils.WorkStp;
import com.smart.wap.bodys.WorkLoginBody;
import com.smart.wap.service.WorkLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 会员资料信息接口
 *
 * @author Administrator
 */
@Api(tags = "喂养端-身份认证")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "work/login")
@RequiredArgsConstructor
@Log4j2
public class WorkLoginController {

    private final WorkLoginService workLoginService;
    private final WxMaService wxMaService;

    /**
     * 小程序登录
     * @param body 体
     * @return {@link AjaxResult}<{@link String}>
     */
    @PostMapping(value = "login")
    @ApiOperation(value = "喂养登录", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<LoginResult> getList(@RequestBody @Validated  WorkLoginBody.LoginBody body) {
        LoginResult login = workLoginService.login(body);
        return AjaxResult.data(login);
    }

    @PostMapping(value = "getLoginCode")
    @ApiOperation(value = "获取手机登录验证码", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<String> getSmsCode(@RequestBody @Validated WorkLoginBody.authCodeBody body) {
        workLoginService.getAuthCode(body);
        return AjaxResult.success();
    }

    @GetMapping(value = "wxLogin")
    @ApiOperation(value = "微信授权码登录", notes = SmartConstant.NOT_NEED_TOKEN )
    public AjaxResult<LoginResult> getAccessToken(@RequestParam(name = "code") String code) {

        try {
            WxMaJscode2SessionResult wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(code);
            String openid = wxMaJscode2SessionResult.getOpenid();
            log.warn("Access token {}", openid);
            LoginResult loginResult = workLoginService.actionWxUser(openid);
            return AjaxResult.data(loginResult);
        } catch (Exception e) {
            throw new SmartException(e.getMessage());
        }

    }

    @PostMapping(value = "smsLogin")
    @ApiOperation(value = "手机验证码登录", notes = SmartConstant.NOT_NEED_TOKEN)
    public AjaxResult<LoginResult> actionSmsLogin(@RequestBody @Validated WorkLoginBody.authCodeLoginBody body) {
        LoginResult loginResult = workLoginService.authCodeLogin(body);
        return AjaxResult.data(loginResult);
    }

    @PostMapping(value = "refreshToken")
    @ApiOperation(value = "刷新身份令牌", notes = SmartConstant.NOT_NEED_TOKEN + "4002 错误码调用")
    public AjaxResult<LoginResult> refreshToken(@RequestBody @Validated WorkLoginBody.RefreshTokenBody body) {
        LoginResult loginResult = workLoginService.refreshToken(body);
        return AjaxResult.data(loginResult);
    }


    @ApiOperation(value = "注销登录")
    @PostMapping("logout")
    @SaCheckLogin(type = WorkStp.TYPE)
    public AjaxResult<String> logout() {
        if (WorkStp.isLogin()) {
            WorkStp.logout();
        }
        return AjaxResult.success();
    }
}
